﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Data;
using DotSpatial.Topology;

namespace GeostatisticalTool.Classes
{
    class CheckDataInputFilter: CheckDataInput
    {
        public string Filterfields { set; get; }

        public CheckDataInputFilter(IFeatureSet originalData, string field, string trans)
            :base(originalData,field,trans)
        {

        }

        public CheckDataInputFilter(IFeatureSet originalData, string field, string trans, bool filter)
             :base(originalData,field,trans,filter)
        {


        }

        private IFeatureSet CreatefilterFeatureSet()
        { 
            IFeatureSet  data1 = new FeatureSet(FeatureType.Point);
            data1.Projection = _originalData.Projection;
            data1.DataTable.Columns.Add(new System.Data.DataColumn(this.Field, typeof(double)));
            data1.DataTable.Columns.Add(new System.Data.DataColumn("Repeat", typeof(string)));
            List<int> listt = _originalData.SelectIndexByAttribute(Filterfields);
           


            foreach (int index in listt)
            {
                IFeature fea1 = _originalData.GetFeature(index);
                IFeature d=data1.AddFeature((IBasicGeometry)fea1);
                d.DataRow[this.Field] = fea1.DataRow[this.Field];
            }


            return data1;
        }

        public override void UpdateDataFeature(string[] problems)
        {
            if (Filterfields == null)
                return;

            IFeatureSet _originalData = CreatefilterFeatureSet();
            if (_originalData == null) 
            return;

            Stat stat = new Stat(false);
            foreach (Feature fea1 in _originalData.Features)
            {
                int i = 0;
                double sum = 0;

                if (fea1.DataRow[this.Field] != null)
                {

                    if (Convert.ToString(fea1.DataRow["Repeat"]) != "R")
                    {




                        foreach (Feature fea2 in _originalData.Features)
                        {
                            if (fea2.DataRow[this.Field] != null)
                            {
                                if (fea1.BasicGeometry.Coordinates[0].X == fea2.BasicGeometry.Coordinates[0].X &&
                                 fea1.BasicGeometry.Coordinates[0].Y == fea2.BasicGeometry.Coordinates[0].Y)
                                {
                                    fea2.DataRow["Repeat"] = "R";
                                    i++;
                                    try
                                    {
                                        sum += Convert.ToDouble(fea1.DataRow[Field]);
                                    }
                                    catch
                                    {
                                        problems[0] = "Empty value";
                                    }
                                }
                            }
                        }

                        try
                        {
                            if (!double.IsNaN(Transform(Trans, sum / i)))
                            {
                                IFeature newFea = data.AddFeature(fea1);
                                newFea.DataRow["Measured"] = sum / i;
                                newFea.DataRow["Trans"] = Transform(Trans, sum / i);
                                //sumAllValues += (sum / i);
                                //quad += ((sum / i) * (sum / i));
                                // nRegisters++;
                                stat += new Stat((sum / i));
                            }
                            else
                            {
                                problems[1] = "Transformation problem (Not zero values allowed)";
                            }
                        }
                        catch
                        {

                        }
                    }
                }
            }
            if (stat.num > 0)
                this.MeanValue = stat.Mean;//sumAllValues / nRegisters;
            this.StandarDev = stat.StdDevS; //Math.Sqrt((quad - (2 * sumAllValues * this.meanValue) + (nRegisters * this.meanValue * this.meanValue)) / nRegisters);
        }


    }
}
